package com.base.controller;

import java.math.BigDecimal;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.base.po.voip.SelfShopOrder;
import com.base.service.SelfShopOrderService;
import com.base.service.ShopClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.base.po.voip.SelfShop;
import com.base.po.voip.Users;
import com.base.service.SelfShopService;
import com.base.service.UsersService;
import com.base.utils.Produce;

/**
 * 自营商城店铺信息
 *
 * @author Administrator
 */
@Controller
@RequestMapping("/admin")
public class index_SelfShop {
    @Autowired
    private SelfShopService selfShopService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private ShopClassifyService shopClassifyService;
    @Autowired
    private SelfShopOrderService selfShopOrderService;

    // 用户首页
    @RequestMapping("/selfShop")
    public ModelAndView index(HttpServletRequest request) throws Exception {
        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");

        Users users = usersService.selectByPrimaryKey(adminUserId);
        List<SelfShop> selfShopList = null;
        if (users.getRole() == 0 || users.getRole() == 1) {
            selfShopList = selfShopService.selectBySelfShop(adminUserId, adminUserId);
            if (selfShopList == null || selfShopList.size() < 1) {
                selfShopService.insertSelective(new SelfShop(Produce.getUUID(), adminUserId,
                        adminUserId, Produce.getNowDate(), (byte) 1, (byte) 1, Produce.getNowDate()));
                selfShopList = selfShopService.selectBySelfShop(adminUserId, adminUserId);
            }
        }
        if (users.getRole() == 2) {
            selfShopList = selfShopService.selectBySelfShop(users.getParentid(), adminUserId);
        }
        SelfShop selfShop = null;
        if (selfShopList != null && selfShopList.size() > 0) {
            selfShop = selfShopList.get(0);
        }
        request.setAttribute("selfShop", selfShop);
        request.setAttribute("adminUsergrade", adminUsergrade);
        if (users.getRole() == 1) {
            request.setAttribute("sClassifyList", shopClassifyService.selectByAll(adminUserId));
        } else {
            request.setAttribute("sClassifyList", shopClassifyService.selectByAll(users.getParentid()));
        }

        // 设置页面进行跳转
        modelAndView.setViewName("jsp/index_SelfShop");
        return modelAndView;
    }

    /**
     * 根据parentId获取该parentId下的所有商家
     *
     * @param request
     * @param parentId
     * @return
     */
    @RequestMapping("/getSelfShops")
    public @ResponseBody
    Map<String, Object>
    index1(HttpServletRequest request, String parentId) {
        String adminUserId = (String) request.getSession().getAttribute("adminUserId"); //根据登陆获取
        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
        Map<String, Object> map = new HashMap<>();
        Users users = usersService.selectByPrimaryKey(adminUserId);
        List<SelfShop> selfShopList = null;
        if (users.getRole() == 0 || users.getRole() == 1) {
            selfShopList = selfShopService.selectBySelfShop(adminUserId, null);
            if (selfShopList == null || selfShopList.size() < 1) {
                map.put("success", true);
                map.put("msg", "空结果");
                map.put("data", selfShopList);
                map.put("total", "0");
            } else {
                map.put("success", true);
                map.put("msg", "传入成功");
                map.put("data", selfShopList);
                map.put("total", selfShopList.size());
            }

        } else {
            map.put("success", false);
            map.put("msg", "参数问题");
            map.put("msg", selfShopList);
            map.put("total", "0");
        }
        return map;

    }

    /**
     * 根据传入的SelfShopId 获取店铺的订单信息，并统计订单信息，将数据筛选出来。
     *
     * @param request
     * @param selfShopId
     * @return
     */
    @RequestMapping("/countSelfShop")
    public @ResponseBody
    Map<String, Object>
    index2(HttpServletRequest request, String selfShopId, String usetime) {
        Map<String, Object> map = new HashMap<>();
        String datetime1 = null;
        String datetime2 = null;
        BigDecimal totalearn = new BigDecimal("0.0");     //总收入
            BigDecimal cash = new BigDecimal("0.0");     //现金支付金额
        BigDecimal derate = new BigDecimal("0.0");     //抵扣额度
        BigDecimal zfbearn = new BigDecimal("0.0");     //支付宝额度
        BigDecimal wxearn = new BigDecimal("0.0");     //微信额度
        BigDecimal unknowearn = new BigDecimal("0.0");     //未知现金收入
        if (usetime != null && usetime != "") {
            String[] arr = usetime.split(" - ");
            datetime1 = arr[0];
            datetime2 = arr[1];
        }

        if (selfShopId == null || selfShopId == "") {
            String adminUserId = (String) request.getSession().getAttribute("adminUserId"); //根据登陆获取
            List<SelfShop> selfShopList = selfShopService.selectBySelfShop(adminUserId, null);
            if (selfShopList != null || selfShopList.size() >= 1) {
                for (int i = 0; i < selfShopList.size(); i++) {
                    selfShopId = selfShopList.get(i).getId();
                    if (selfShopId!=null){
                        List<SelfShopOrder> selfShopOrderList = selfShopOrderService.selectAllbyDatetime(selfShopId, datetime1, datetime2);
                        map.put("total", selfShopOrderList.size());
                        for (SelfShopOrder s : selfShopOrderList) {
                            //根据获取的SelfShopOrder获得参数
                            totalearn = s.getCash().add(s.getDerate()).add(totalearn); //总收入=现金支付+抵扣金额
                            cash = s.getCash().add(cash);                              //现金金额相加
                            derate = s.getDerate().add(derate);                        //抵扣金额相加
                            Integer a = s.getPaymethod();                              //1.支付宝，2.微信，3话费全抵。
                            if (a != null) {
                                switch (a) {
                                    case 1:
                                        zfbearn = s.getCash().add(zfbearn);
                                        break;
                                    case 2:
                                        wxearn = s.getCash().add(wxearn);
                                        break;
                                    case 3:
                                        break;
                                    default:
                                        unknowearn = s.getCash().add(unknowearn);
                                        break;
                                }
                            } else {
                                unknowearn = s.getCash().add(unknowearn);
                            }
                        }
                    }
                }
            } else {
                map.put("msg", "查询为空");
                map.put("success", true);
                map.put("data", "");
                map.put("total", "0");
            }
        } else {
            List<SelfShopOrder> selfShopOrderList = selfShopOrderService.selectAllbyDatetime(selfShopId, datetime1, datetime2);
            if (selfShopOrderList == null || selfShopOrderList.size() < 1) {
                map.put("msg", "查询为空");
                map.put("success", true);
                map.put("data", selfShopOrderList);
                map.put("total", "0");
            } else {
                map.put("msg", "查询成功");
                map.put("success", true);
                map.put("data", selfShopOrderList);
                map.put("total", selfShopOrderList.size());
                for (SelfShopOrder s : selfShopOrderList) {
                    //根据获取的SelfShopOrder获得参数
                    totalearn = s.getCash().add(s.getDerate()).add(totalearn); //总收入=现金支付+抵扣金额
                    cash = s.getCash().add(cash);                  //现金金额相加
                    derate = s.getDerate().add(derate);            //抵扣金额相加
                    Integer a = s.getPaymethod();                      //1.支付宝，2.微信，3话费全抵。
                    if (a != null) {
                        switch (a) {
                            case 1:
                                zfbearn = s.getCash().add(zfbearn);
                                break;
                            case 2:
                                wxearn = s.getCash().add(wxearn);
                                break;
                            case 3:
                                break;
                            default:
                                unknowearn = s.getCash().add(unknowearn);
                                break;
                        }
                    } else {
                        unknowearn = s.getCash().add(unknowearn);
                    }
                }
            }
        }
        map.put("totalearn", totalearn);
        map.put("cash", cash);
        map.put("derate", derate);
        map.put("zfbearn", zfbearn);
        map.put("wxearn", wxearn);
        map.put("unknowearn", unknowearn);
        return map;
    }

    // 自营商城首页
    @RequestMapping("/selfShopCount")
    public ModelAndView selfShopCount(HttpServletRequest request) {
        // 返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");

//        Users users = usersService.selectByPrimaryKey(adminUserId);
//        List<SelfShop> selfShopList = null;
//        if(users.getRole() == 0 || users.getRole() == 1) {
//            selfShopList = selfShopService.selectBySelfShop(adminUserId,adminUserId);
//            if(selfShopList ==null || selfShopList.size()<1) {
//                selfShopService.insertSelective(new SelfShop(Produce.getUUID(), adminUserId,
//                        adminUserId, Produce.getNowDate(), (byte)1, (byte)1, Produce.getNowDate()));
//                selfShopList = selfShopService.selectBySelfShop(adminUserId,adminUserId);
//            }
//        }
//        if(users.getRole() == 2) {
//            selfShopList = selfShopService.selectBySelfShop(users.getParentid(),adminUserId);
//        }
//        SelfShop selfShop = null;
//        if(selfShopList != null && selfShopList.size() > 0) {
//            selfShop = selfShopList.get(0);
//        }
//        request.setAttribute("selfShop", selfShop);
//        request.setAttribute("adminUsergrade", adminUsergrade);
//        if (users.getRole() == 1){
//            request.setAttribute("sClassifyList", shopClassifyService.selectByAll(adminUserId));
//        }else{
//            request.setAttribute("sClassifyList", shopClassifyService.selectByAll(users.getParentid()));
//        }

        // 设置页面进行跳转
        modelAndView.setViewName("jsp/countSelfShopOrder");
        return modelAndView;
    }
}
